package cn.mdx.service;

import cn.mdx.common.beans.User;
import cn.mdx.common.utils.CheckUtil;
import cn.mdx.mapper.DemandMapper;
import cn.mdx.mapper.DemandlistMapper;
import cn.mdx.mapper.ResultMapper;
import cn.mdx.pojo.Demand;
import cn.mdx.pojo.DemandVO;
import cn.mdx.pojo.Demandlist;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@Service
@Transactional(rollbackFor = Exception.class)
public class DemandService {

    private static final Logger logger = LoggerFactory.getLogger(DemandService.class);

    @Autowired
    private DemandMapper demandMapperd;

    @Autowired
    private DemandlistMapper demandlistMapper;

    @Autowired
    private ResultMapper resultMapper;

    /**
     *
     * @param demand 需求信息
     * @param uid 用户id
     */
    public void addDemand(Demand demand, Integer uid) {
        CheckUtil.mustNullOtherOpposite(demand,"添加异常","audit&did");
        CheckUtil.notNull(uid,"添加异常");
        logger.info("[开始发布需求,参数[{}]]",demand);
        demandMapperd.insertSelective(demand);
        Demandlist demandlist = new Demandlist();
        demandlist.setDid(demand.getDid());
        demandlist.setNid(uid);
        demandlistMapper.insertSelective(demandlist);
        logger.info("[发布成功]");
    }

    /**
     *
     * @param id 需求id
     * @param uid 用户id
     * @return 详细需求信息
     */
    public Demand getDemand(Integer id,Integer uid) {
        CheckUtil.notNull(id,"参数为空");
        CheckUtil.notNull(uid,"参数异常");
        logger.info("[查找中,参数[id:{}]]",id);
        Demandlist demandlist = demandlistMapper.selectByPrimaryKey(id);
        if(demandlist==null) {
            logger.info("[查找失败]");
            CheckUtil.fail("查找失败");
        }
        if(!uid.equals(demandlist.getNid())) {
            logger.info("[查找失败,非法访问]");
            CheckUtil.fail("非法访问");
        }
        Demand demand = demandMapperd.selectByPrimaryKey(demandlist.getDid());
        demand.setDid(demandlist.getId());
        logger.info("[查找成功]");
        return demand;
    }

    public Demand getDemand(Integer id){
        return demandMapperd.selectByPrimaryKey(id);
    }



    public PageInfo<Demand> getDemands(Integer page, String info) {
        CheckUtil.notNull(page,"页码为空");
        PageHelper.startPage(page,10,"did desc");
        PageInfo<Demand> pageInfo = new PageInfo<Demand>(demandMapperd.getDemands(info));
        return pageInfo;
    }


    /**
     *
     * @param demand 需求信息
     * @param uid 用户id
     */
    public void setDemand(Demand demand,Integer uid) {
        CheckUtil.mustNullOtherOpposite(demand,"修改异常","audit");
        CheckUtil.notNull(uid,"修改异常");
        logger.info("[开始修改需求,参数[{}]]",demand);
        Demandlist demandlist = demandlistMapper.selectByPrimaryKey(demand.getDid());
        if(demandlist==null) {
            logger.info("[修改失败]");
            CheckUtil.fail("查找失败");
        }
        if(!uid.equals(demandlist.getNid())) {
            logger.info("[修改失败，非法访问]");
            CheckUtil.fail("非法访问");
        }
        if(resultMapper.findResultByDid(demand.getDid())!=null) {
            logger.info("[修改失败，非法访问]");
            CheckUtil.fail("该需求已对接，不可修改");
        }
        demand.setDid(demandlist.getId());
        demand.setAudit((byte)0);
        demandMapperd.updateByPrimaryKeySelective(demand);
        logger.info("[修改成功]");
    }

    /**
     *
     * @param id 需求id
     * @param uid 用户id
     */
    public void delDemand(Integer id,Integer uid) {
        CheckUtil.notNull(id,"参数为空");
        logger.info("[删除中,参数[id:{}]]",id);
        Demandlist demandlist = demandlistMapper.selectByPrimaryKey(id);
        if(demandlist==null) {
            logger.info("[删除失败]");
            CheckUtil.fail("删除失败");
        }

        if(!uid.equals(demandlist.getNid())) {
            logger.info("[删除失败，非法访问]");
            CheckUtil.fail("非法访问");
        }
        if(resultMapper.findResultByDid(id)!=null) {
            logger.info("[删除失败，非法访问]");
            CheckUtil.fail("该需求已对接，不可删除");
        }
        demandMapperd.deleteByPrimaryKey(demandlist.getDid());
        demandlistMapper.deleteByPrimaryKey(demandlist.getId());
        logger.info("[删除成功]");
    }


    public ArrayList<Integer> getDemandsId(Integer nid) {
        CheckUtil.notNull(nid,"参数为空");
        return demandlistMapper.getDemandsId(nid);
    }


}
